package xujiwen.transport.Mgr;

import java.util.ArrayList;
import java.util.List;

import xujiwen.transport.bean.ChargeSTD;
import xujiwen.transport.util.DBConnect;

public class ChargeSTDMgr {
	public void Save(final ChargeSTD std) {
		
		final DBConnect db = new DBConnect();
		final String sql = "insert into CHARGESTD values (null,?, ?, now())";
		db.setAutoCommit(false);
		db.prepareStatement(sql);
		try {
			final double price = std.getPrice();
			final int carClass = std.getCarClass();
			db.setInt(1, carClass);
			db.setDouble(2, price);
			
			db.excute();
			db.commit();

		} catch (final Exception ex) {
			ex.printStackTrace();
		} finally {
			db.setAutoCommit(true);
			db.close();
		}
	}
	/*public ChargeSTD GetDeatilUser(final int uid) {
		ChargeSTD std = null;
		final DBConnect db = new DBConnect();
		final String sql = "select * from USER where UID = ?";
		db.prepareStatement(sql);
		db.setInt(1, uid);
		db.excuteQuery();
		try {
			while (db.next()) {
				std = new ChargeSTD();
				std.setUid(db.getInt("UID"));
				std.setName(db.getString("NAME"));
				std.setPassword(db.getString("PASSWORD"));
				std.setUclass(db.getInt("CLASS"));
				std.setSex(db.getString("SEX"));
			}
		} catch (final Exception ex) {
			ex.printStackTrace();
			return null;
		} finally {
			db.close();
		}
		return std;
	}*/
	
	public void Update(final ChargeSTD std) {
		final DBConnect db = new DBConnect();
		final String sql = "update CHARGESTD set CHANGEDATE=now(), CARCLASS=?, PRICE=? where CSID=?";
		db.setAutoCommit(false);
		db.prepareStatement(sql);
		try {
			db.setInt(1, std.getCarClass());
			db.setDouble(2, std.getPrice());
			db.setInt(3, std.getCsid());
			///
			//System.out.println(sql);
			db.excute();
			db.commit();
		} catch (final Exception ex) {
			ex.printStackTrace();
		} finally {
			db.setAutoCommit(true);
			db.close();
		}
	}
	
	public List<ChargeSTD> GetSTDList() {
		final List<ChargeSTD> stds = new ArrayList<ChargeSTD>();
		final DBConnect db = new DBConnect();
		final String sql = "select * from CHARGESTD";
		//System.out.println(sql);
		db.prepareStatement(sql);
		db.excuteQuery();
		try {
			while (db.next()) {
				final ChargeSTD std = new ChargeSTD();
				std.setCsid(db.getInt("CSID"));
				std.setPrice(db.getDouble("PRICE"));
				std.setCarClass(db.getInt("CARCLASS"));
				std.setUpdateDate(db.getTimestamp("CHANGEDATE"));
				stds.add(std);
			}
		} catch (final Exception ex) {
			ex.printStackTrace();
			return null;
		} finally {
			db.close();
		}
		return stds;
	}
	
	public void Del(final int csid)
	{
		final DBConnect db = new DBConnect();
		final String sql = "delete from CHARGESTD where CSID = ?";
		db.setAutoCommit(false);
		db.prepareStatement(sql);
		try {
			db.setInt(1, csid);
			db.excute();
			db.commit();
		} catch (final Exception ex) {
			ex.printStackTrace();
		} finally {
			db.setAutoCommit(true);
			db.close();
		}
	}
}
